package com.example.taobao.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class UserDataDpSQLHelp extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "userdata.db";
    private SQLiteDatabase db;

    final String DB_userData = "create table userdata(" +
            "username integer primary key," +
            "password text," +
            "vipName text," +
            "iphone text," +
            "address text)";

    public UserDataDpSQLHelp(@Nullable Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }




    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {


        sqLiteDatabase.execSQL(DB_userData);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    public void insert_user(userdata username){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("username",username.getUsername());
        values.put("password",username.getPassword());
        values.put("vipName",username.getVipName());
        values.put("iphone",username.getIphone());
        values.put("address",username.getPassword());

        sqLiteDatabase.insert("userdata",null,values);
        sqLiteDatabase.close();
    }

    public int delete_user(String name){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        int result = sqLiteDatabase.delete("userdata","username=?",
                new String[]{name});
        return result;
    }

    public int update_user(userdata username){
        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        String name = username.getUsername();

        ContentValues values = new ContentValues();
        values.put("username",username.getUsername());
        values.put("password",username.getPassword());
        values.put("vipName",username.getVipName());
        values.put("iphone",username.getIphone());
        values.put("address",username.getPassword());

        int result = sqLiteDatabase.update("userdata",values,"username=?",new String[]{name});
        sqLiteDatabase.close();
        return  result;

    }

    public Cursor query_user(String name){

        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
        Cursor cursor = sqLiteDatabase.query("userdata",null,"iphone LIKE ?",new String[]{"%"+name+"%"},
                null,null,null,null);
        return cursor;
//        SQLiteDatabase sqLiteDatabase = getWritableDatabase();
//        Cursor cursor = sqLiteDatabase.query("usernames",null,"name LIKE ?",new String[]{"%"+name+"%"},
//                null,null,null,null);
//        return cursor;
    }
}
